home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / doc / regexp.n < prev    next >
Text File  |  1993-06-17  |  7KB  |  148 lines

  1. '\"
  2. '\" Copyright (c) 1993 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/tcl/man/RCS/regexp.n,v 1.2 93/06/17 13:31:37 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. .so man.macros
  24. .HS regexp tcl
  25. .BS
  26. '\" Note:  do not modify the .SH NAME line immediately below!
  27. .SH NAME
  28. regexp \- Match a regular expression against a string
  29. .SH SYNOPSIS
  30. \fBregexp \fR?\fIswitches\fR? \fIexp string \fR?\fImatchVar\fR? ?\fIsubMatchVar subMatchVar ...\fR?
  31. .BE
  32.  
  33. .SH DESCRIPTION
  34. .PP
  35. Determines whether the regular expression \fIexp\fR matches part or
  36. all of \fIstring\fR and returns 1 if it does, 0 if it doesn't.
  37. .LP
  38. If additional arguments are specified after \fIstring\fR then they
  39. are treated as the names of variables in which to return
  40. information about which part(s) of \fIstring\fR matched \fIexp\fR.
  41. \fIMatchVar\fR will be set to the range of \fIstring\fR that
  42. matched all of \fIexp\fR.  The first \fIsubMatchVar\fR will contain
  43. the characters in \fIstring\fR that matched the leftmost parenthesized
  44. subexpression within \fIexp\fR, the next \fIsubMatchVar\fR will
  45. contain the characters that matched the next parenthesized
  46. subexpression to the right in \fIexp\fR, and so on.
  47. .LP
  48. If the initial arguments to \fBregexp\fR start with \fB\-\fR then
  49. .VS
  50. they are treated as switches.  The following switches are
  51. currently supported:
  52. .TP 10
  53. \fB\-nocase\fR
  54. Causes upper-case characters in \fIstring\fR to be treated as
  55. lower case during the matching process.
  56. .TP 10
  57. \fB\-indices\fR
  58. Changes what is stored in the \fIsubMatchVar\fRs. 
  59. Instead of storing the matching characters from \fBstring\fR,
  60. each variable
  61. will contain a list of two decimal strings giving the indices
  62. in \fIstring\fR of the first and last characters in the matching
  63. range of characters.
  64. .TP 10
  65. \fB\-\|\-\fR
  66. Marks the end of switches.  The argument following this one will
  67. be treated as \fIexp\fR even if it starts with a \fB\-.
  68. .VE
  69. .LP
  70. If there are more \fIsubMatchVar\fR's than parenthesized
  71. subexpressions within \fIexp\fR, or if a particular subexpression
  72. in \fIexp\fR doesn't match the string (e.g. because it was in a
  73. portion of the expression that wasn't matched), then the corresponding
  74. \fIsubMatchVar\fR will be set to ``\fB\-1 \-1\fR'' if \fB\-indices\fR
  75. has been specified or to an empty string otherwise.
  76.  
  77. .SH "REGULAR EXPRESSIONS"
  78. .PP
  79. Regular expressions are implemented using Henry Spencer's package
  80. (thanks, Henry!),
  81. and the description of regular expressions below is copied verbatim
  82. from his manual entry.
  83. .PP
  84. A regular expression is zero or more \fIbranches\fR, separated by ``|''.
  85. It matches anything that matches one of the branches.
  86. .PP
  87. A branch is zero or more \fIpieces\fR, concatenated.
  88. It matches a match for the first, followed by a match for the second, etc.
  89. .PP
  90. A piece is an \fIatom\fR possibly followed by ``*'', ``+'', or ``?''.
  91. An atom followed by ``*'' matches a sequence of 0 or more matches of the atom.
  92. An atom followed by ``+'' matches a sequence of 1 or more matches of the atom.
  93. An atom followed by ``?'' matches a match of the atom, or the null string.
  94. .PP
  95. An atom is a regular expression in parentheses (matching a match for the
  96. regular expression), a \fIrange\fR (see below), ``.''
  97. (matching any single character), ``^'' (matching the null string at the
  98. beginning of the input string), ``$'' (matching the null string at the
  99. end of the input string), a ``\e'' followed by a single character (matching
  100. that character), or a single character with no other significance
  101. (matching that character).
  102. .PP
  103. A \fIrange\fR is a sequence of characters enclosed in ``[]''.
  104. It normally matches any single character from the sequence.
  105. If the sequence begins with ``^'',
  106. it matches any single character \fInot\fR from the rest of the sequence.
  107. If two characters in the sequence are separated by ``\-'', this is shorthand
  108. for the full list of ASCII characters between them
  109. (e.g. ``[0-9]'' matches any decimal digit).
  110. To include a literal ``]'' in the sequence, make it the first character
  111. (following a possible ``^'').
  112. To include a literal ``\-'', make it the first or last character.
  113. .PP
  114. If a regular expression could match two different parts of a string,
  115. it will match the one which begins earliest.
  116. If both begin in the same place but match different lengths, or match
  117. the same length in different ways, life gets messier, as follows.
  118. .PP
  119. In general, the possibilities in a list of branches are considered in
  120. left-to-right order, the possibilities for ``*'', ``+'', and ``?'' are
  121. considered longest-first, nested constructs are considered from the
  122. outermost in, and concatenated constructs are considered leftmost-first.
  123. The match that will be chosen is the one that uses the earliest
  124. possibility in the first choice that has to be made.
  125. If there is more than one choice, the next will be made in the same manner
  126. (earliest possibility) subject to the decision on the first choice.
  127. And so forth.
  128. .PP
  129. For example, ``(ab|a)b*c'' could match ``abc'' in one of two ways.
  130. The first choice is between ``ab'' and ``a''; since ``ab'' is earlier, and does
  131. lead to a successful overall match, it is chosen.
  132. Since the ``b'' is already spoken for,
  133. the ``b*'' must match its last possibility\(emthe empty string\(emsince
  134. it must respect the earlier choice.
  135. .PP
  136. In the particular case where no ``|''s are present and there is only one
  137. ``*'', ``+'', or ``?'', the net effect is that the longest possible
  138. match will be chosen.
  139. So ``ab*'', presented with ``xabbbby'', will match ``abbbb''.
  140. Note that if ``ab*'' is tried against ``xabyabbbz'', it
  141. will match ``ab'' just after ``x'', due to the begins-earliest rule.
  142. (In effect, the decision on where to start the match is the first choice
  143. to be made, hence subsequent choices must respect it even if this leads them
  144. to less-preferred alternatives.)
  145.  
  146. .SH KEYWORDS
  147. match, regular expression, string
  148.